package com.hfdg.authority.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfdg.authority.model.AuthMenuTreeNode;
import com.hfdg.prototype.base.utils.TimeUtils;
import com.hfdg.authority.mapper.AuthMenuMapper;
import com.hfdg.authority.entity.AuthMenu;
import com.hfdg.authority.service.AuthMenuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName AuthMenuServiceImpl
 * @Description TODO
 * @Author yuelh
 * @Date 2019/1/19 18:26
 * @Version 1.0
 * @Company 会飞的锅工作室
 **/
@Service
public class AuthMenuServiceImpl implements AuthMenuService {
    @Autowired
    AuthMenuMapper authMenuMapper;

    @Override
    public String insert(AuthMenu authMenu) {
        String uid = UUID.randomUUID().toString();
        authMenu.setUid(uid);
        authMenu.setDelFlag("0");
        authMenu.setCreateTime(new Date());
        authMenuMapper.insert(authMenu);
        return uid;
    }

    @Override
    public Integer update(AuthMenu authMenu) {
        authMenu.setUpdateTime(new Date());
        return authMenuMapper.update(authMenu);
    }

    @Override
    public Integer  delete(String uid) {
        return authMenuMapper.delete(uid);
    }
    @Override
    public Integer batchDelete(String[] uids) {
            return authMenuMapper.batchDelete(uids);
    }

    @Override
    public AuthMenu get(String uid) {
        return authMenuMapper.get(uid);
    }

    @Override
    public List<AuthMenu> list(AuthMenu authMenu) {
    return  authMenuMapper.list(authMenu);
    }

    @Override
    public PageInfo<Map<String,Object>> getPagingData(int offset, int limit,String orderBy, AuthMenu authMenu) {
        if(null==orderBy|| StringUtils.isBlank(orderBy)){
        PageHelper.startPage((offset/limit)+1,limit);
        }else{
        PageHelper.startPage((offset/limit)+1,limit,orderBy);
        }
        PageInfo<Map<String,Object>> pageInfo =new PageInfo<>(authMenuMapper.listMap(authMenu));
            return pageInfo;
            }
    @Override
    public String insertOrUpdate(AuthMenu authMenu){
        if(null!=authMenu.getUid()){
            this.update(authMenu);
        }else{
            this.insert(authMenu);
        }
        return authMenu.getUid();
    }
    @Override
    public List<Map<String, Object>> listMenuTree(AuthMenu authMenu) {
        return authMenuMapper.listMenuTree(authMenu);
    }
    @Override
    public List<AuthMenuTreeNode> buildLeftMenuBar(AuthMenu authMenu) {
        List<AuthMenu > authMenuList = authMenuMapper.list(authMenu);
        return AuthMenuTreeNode.buildTreeData(authMenuList);
    }
}
